package com.example.cet4try.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.cet4try.R;

public class ActivitySelectDataBase extends AppCompatActivity {

    private TextView selected_dbname;
    private TextView[] selects;
    private TextView temp = null;
    private boolean h_cet6 = false,h_ky = false;
    private int select_DB_Id;
    private SharedPreferences preferences;
    private Drawable bg0,bg1;
    private String[] db_list = new String[]{"英语四级单词","英语六级单词","考研英语单词"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_data_base);

        initView();

        preferences = getSharedPreferences("App_Shared_Data", 0);
        select_DB_Id = preferences.getInt("SelectDB",0);

        selected_dbname.setText(db_list[select_DB_Id]);

        bg0 = getResources().getDrawable(R.drawable.button_select_db);
        bg1 = getResources().getDrawable(R.drawable.button_select_db2);

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int n = (int) view.getTag();
                selected_dbname.setText(db_list[n]);
                if (temp != null){
                    temp.setBackground(bg0);
                    temp.setText("选择");
                }

                temp = (TextView) view;
                temp.setBackground(bg1);
                temp.setText("已选");
                SharedPreferences.Editor editor = preferences.edit();
                editor.putInt("SelectDB",n);
                editor.apply();

            }
        };

        for (int i = 0;i<3;i++){
            selects[i].setOnClickListener(listener);
        }

        temp = selects[select_DB_Id];
        temp.setBackground(bg1);
        temp.setText("已选");

    }

    private void initView() {
        selected_dbname = findViewById(R.id.selected_dbname);
        selects = new TextView[]{findViewById(R.id.select_cet4),findViewById(R.id.select_cet6),findViewById(R.id.select_ky)};
        for (int i = 0;i<3;i++){
            selects[i].setTag(i);
        }
    }


}
